package com.itstyle.seckill.common.designPattern.zhongjie;

import java.util.ArrayList;
import java.util.List;

public class ChatRoom {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
    private int userCount = 0;
    public ChatRoom(String name) {
        this.name = name;
    }

    List<User> users = new ArrayList<>();

    public void connect(User user) {
        if(this.users.contains(user)){
            System.out.println("n你已经在凡间中");
            return;
        }
        this.users.add(user);
        System.out.print("欢迎" + user.getName() + "加入聊天室【" + name + "】--------------》");
        userCount++;
        System.out.println("当前登陆用户数量：【"+userCount+"】");
    }

    public void loginOut(User user){
        this.users.remove(user);
        System.out.println("用户-->"+user.getName() + "退出聊天室【" + name + "】");
        userCount--;
        System.out.println("当前登陆用户数量："+userCount);
    }

    public void sendMsg(User fromUser,String msg){
        users.stream().filter(user -> !user.equals(fromUser)).forEach(toWho -> toWho.Listen(fromUser,msg));
    }

    public static void main(String[] args) {
        ChatRoom room = new ChatRoom("qq聊天室");
        ChatRoom room1 = new ChatRoom("qq1聊天室");
        User u1 = new User("阿里巴巴");
        User u2 = new User("腾讯");
        User u3 = new User("美团");
        User u4 = new User("百度");
        u1.login(room);
        u2.login(room);
        u3.login(room);
        u1.sendMsg(room,"我是阿里巴巴");
        u2.sendMsg(room,"我是腾讯");
        u3.login(room1);
        u4.login(room1);
        u3.sendMsg(room1,"我是美团");
        u3.sendMsg(room,"我是美团");
    }
}
